﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;

namespace GreenTeam.SharePointOnline
{
  [ToolboxItemAttribute(false)]
  public class DigiClockWebPart : WebPart
  {
    [WebBrowsable, Personalizable(PersonalizationScope.Shared)]
    public Boolean EmbedJquery { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {
      // Register the JS and CSS files.
      var siteUrl = SPContext.Current.Site.ServerRelativeUrl;
      if (!siteUrl.EndsWith("/")) siteUrl += "/";
      var markup = new StringBuilder();
      markup.Append(@"<div id='digiclock'></div>");
      if (this.EmbedJquery)
      {
        markup.AppendFormat(
          @"<script type='text/javascript' src='{0}SiteAssets/DigiClock/Js/jquery-1.3.2.min.js'></script>",
          siteUrl);
      }
      markup.AppendFormat(@"        
        <script type='text/javascript' src='{0}SiteAssets/DigiClock/Js/jquery.jdigiclock.js'></script>        
        <script type='text/javascript'>
        jQuery('head').append(""<link rel='stylesheet' type='text/css' href='{0}SiteAssets/DigiClock/Css/jquery.jdigiclock.css' />"");  
        jQuery('#digiclock').jdigiclock({{clockImagesPath:""{0}SiteAssets/DigiClock/images/clock/"",imagesPath:""{0}SiteAssets/DigiClock/images/"",}});
        </script>", siteUrl);
      writer.Write(markup.ToString());
    }
  }
}
